from sqlalchemy import create_engine, Column, String, Float
from sqlalchemy.orm import declarative_base, sessionmaker
import pandas as pd

Base = declarative_base()

class ConvertibleBond(Base):
    __tablename__ = 'convertible_bonds'
    bond_code = Column(String(10), primary_key=True)
    bond_name = Column(String(50))
    price = Column(Float)
    premium_rate = Column(Float)
    volume = Column(Float)

class SQLiteStorage:
    def __init__(self, db_path='data/bonds.db'):
        self.engine = create_engine(f'sqlite:///{db_path}')
        Base.metadata.create_all(self.engine)
        self.Session = sessionmaker(bind=self.engine)

    def save(self, data):
        df = pd.DataFrame(data)
        df.to_sql('convertible_bonds', con=self.engine, if_exists='append', index=False)

    def query_all(self):
        session = self.Session()
        try:
            return [bond.__dict__ for bond in session.query(ConvertibleBond).all()]
        finally:
            session.close()